#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "stock.h"
#include "llist.h"
#include "md5.h"

char adm_name[21]="admin";   //管理员用户名
char adm_pass[21]="888888"; //管理员初始密码

static int user_sum = 0;    //用户数量
static int stuff_sum =0;    //物资种类数量

LLIST *llist_user = NULL;
LLIST *llist_stuff = NULL;

//用户名比较函数，相同返回0，不同返回非0值
static int user_name_cmp(const void *key,const void *record)
{
    const char *name = key;
    user const *u = record;

    return strcmp(name,u->u_name);
}

static int stuff_name_cmp(const void *key,const void *record)
{
    const char *name = key;
    stuff const *s = record;

    return strcmp(name,s -> stuff_name);
}


//用户/管理员 更新用户UI
static void _updata_user_UI(user *u)
{
    int m;
    char name[21];
    char sex[4];
    char phone[12];
    char id[21];
    char passwd[21];
    while(1){
        printf("\033c");
        printf("##########更新用户界面##########\n");
        printf("    请选择下列选项进行修改：    \n");
        printf("   1、姓名：%s\n",u->u_name);
        printf("   2、性别：%s\n",u->u_sex);
        printf("   3、手机号：%s\n",u->u_mo_num);
        printf("   4、身份证：%s\n",u->u_id);
        printf("   5、密码：%s\n",u->u_passwd);
        printf("   6、退出\n");
        printf("################################\n");
		scanf("%d",&m);
		switch(m){
			case 1:
				printf("请输入新的姓名：");
                scanf("%s",name);
                strcpy(u->u_name,name);
				break;
			case 2:
                printf("请输入新的性别：");
                scanf("%s",sex);
                strcpy(u->u_sex,sex);
				break;
			case 3:
                printf("请输入新的手机号：");
                scanf("%s",phone);
                strcpy(u->u_mo_num,phone);
				break;
			case 4:
				printf("请输入新的身份证号：");
                scanf("%s",id);
                strcpy(u->u_id,id);
				break;
			case 5:
				printf("请输入新的登陆密码：");
                scanf("%s",name);
                strcpy(u->u_passwd,passwd);
				break;
            case 6:
                return;
                printf("\033c");
			default:
				printf("输入错误\n");
				break;
		}
	}
}



//管理员 添加新用户
void adm_add_user()
{
    user new_user;

    printf("请输入新用户的工号：");
    scanf("%d",&new_user.job_num);
    printf("请输入新用户的姓名：");
    scanf("%s",new_user.u_name);
    printf("请输入新用户的性别：");
    scanf("%s",new_user.u_sex);
    printf("请输入新用户的手机号：");
    scanf("%s",new_user.u_mo_num);
    printf("请输入新用户的身份证号：");
    scanf("%s",new_user.u_id);
    //new_user.llist_stuff = NULL;
    new_user.llist_stuff = llist_create(sizeof(stuff));

    memcpy(new_user.u_passwd,(new_user.u_id) + 12, 7);
    
    int flag = llist_insert(llist_user,&new_user);
    
    if(flag == -1)
    {
        perror("insert_user");
        printf("添加用户失败！\n");
    }
    user_sum++;
    printf("\033c");
    printf("添加用户成功！\n");
    //new_user ->llist_stuff_info = llist_create(sizeof(stuff_info));
    
    sleep(1);
    printf("\033c");  
    
}


//管理员 删除用户
void adm_delete_user()
{
    printf("请输入被删除用户名：");
    char name[21];
    scanf("%s",name);
    struct llist_node *code;
    code = llist_find(llist_user,name,user_name_cmp);
    printf("\033c");
    if(code == NULL)
    {
        printf("查无此人\n");
        sleep(1);
        printf("\033c");
    }
    else
    {
        user *u = (user*)code -> data;
        if(u->llist_stuff == NULL)
        {
            int flag = llist_delete(llist_user,name,user_name_cmp);//删除节
            if(flag == 0) 
                printf("删除用户成功！\n");
                sleep(1);
                printf("\033c");
            user_sum--;
        }
        else
        {
            printf("请归还完全部物资后删除！\n");
            sleep(1);
            printf("\033c");
        }
    }

}


//管理员 更新用户信息
void adm_updata_user()
{
	printf("请输入需要更新的用户名：");
    char name[21];
    scanf("%s",name);
    user *u;
    u = llist_find(llist_user,name,user_name_cmp);
    
    if(u == NULL)
    {
        printf("查无此人\n");
        sleep(1);
    }
    else
    {
        _updata_user_UI(u);
    }
}


//管理员 查找用户
void find_user()
{
    printf("请输入用户名：");
    char name[21];
    scanf("%s",name);
    user *u;
    u = llist_find(llist_user,name,user_name_cmp);
    printf("\033c");
    if(u == NULL)
    {
        printf("查无此人\n");
        sleep(1);
        printf("\033c");
    }
    else
    {
        
        printf("工号：%d\n",u->job_num);
        printf("姓名：%s\n",u->u_name);
        printf("性别：%s\n",u->u_sex);
        printf("手机号：%s\n",u->u_mo_num);
        printf("身份证：%s\n",u->u_id);
        printf("密码：%s\n\n",u->u_passwd);
        printf("6s后自动退出界面....\n");
        sleep(6);

        printf("\033c");
    }
}


//管理员 查询全部用户
void find_all_user()
{
    struct llist_node *cur = llist_user -> head.next;
    user *u;
    while (cur != &llist_user -> head)
    {
        u = (user *)cur -> data;
        printf("工号：%d  姓名：%s  性别：%s  手机号：%s\n",u->job_num,u->u_name,u->u_sex,u->u_mo_num);
        cur = cur -> next;
    }
    printf("3s后自动退出...\n");
    sleep(3);
    printf("\033c");
}


//管理员 添加物资
void add_stuff()
{
    stuff new_stuff;

    printf("请输入物资名称：");
    scanf("%s",new_stuff.stuff_name);
    printf("请输入物资编号：");
    scanf("%s",new_stuff.stuff_id);
    printf("请输入物资数量：");
    scanf("%d",&new_stuff.stuff_sum);
    new_stuff.stuff_num = new_stuff.stuff_sum;
    //stuff *s = llist_find(llist_stuff,new_stuff.stuff_name,stuff_name_cmp);
    //if(s == NULL)
    //{
        int flag = llist_insert(llist_stuff,&new_stuff);
        
        if(flag == 0)
        {
            stuff_sum++;
            printf("\033c");
            printf("添加物资成功！\n");
            sleep(1);
            printf("\033c");
        }   
        
        else
        {
            printf("添加物资失败！\n");
        }
    }

}


//管理员 删除物资
void delete_stuff()
{
    if(stuff_sum == 0)
    {
        printf("仓库内无物资！\n");
    }
    if(stuff_sum > 0)
    {
        printf("请输入被删除物资名：");
        char name[15];
        scanf("%s",name);
        struct llist_node *code;
        code = llist_find(llist_stuff,name,stuff_name_cmp);
        printf("\033c");
        if(code == NULL)
        {
            printf("库内无此物资，删除失败！\n");
            sleep(1);
            printf("\033c");
        }
        else
        {
            stuff *s = (stuff*)code;
            int flag = llist_delete(llist_stuff,s ->stuff_name,stuff_name_cmp);//删除节
                if(flag == 0) 
                {
                    printf("删除物资成功！\n");
                    sleep(1);
                    printf("\033c");
                    stuff_sum--;
                }

        }
    }
}


//管理员 查看库存
void find_all_stuff()
{
    struct llist_node *cur = llist_stuff -> head.next;
    stuff *s;
    
    while (cur != &llist_stuff -> head)
    {    
        s = (stuff *)cur -> data;
        printf("名称：%s  总数量：%d  剩余数量：%d  编号：%s\n",s->stuff_name,s->stuff_sum,s->stuff_num,s->stuff_id);
        cur = cur -> next;
    }
    printf("3s后自动退出...\n");
    sleep(3);
    printf("\033c");
}


//普通用户 借物资
int user_bor(user *u)
{

    printf("请输入物资名：");
    char name[15];
    scanf("%s",name);
    stuff *s = llist_find(llist_stuff,name,stuff_name_cmp);
    printf("\033c");
    if(s == NULL)
    {
        printf("无此物资！\n");
        sleep(1);
        printf("\033c");
    }
    else if(s -> stuff_sum == 0)
    {
        printf("此物资数量为 0，请提醒管理员补货！\n");
        sleep(1);
        printf("\033c");
    }
    else
    {
        printf("名称：%s  总数量：%d  剩余：%d  编号：%s\n",s->stuff_name,s->stuff_sum,s->stuff_num,s->stuff_id);
        int num;
        printf("请输入需借上述数量：");
        scanf("%d",&num);
        if(num <= s ->stuff_sum)
        {   
            /*
            stuff_info *new_stuff_info;

            strcpy(new_stuff_info -> stuff_name, s-> stuff_name);
            strcpy(new_stuff_info -> stuff_id, s -> stuff_id);
            new_stuff_info -> stuff_sum = num;
            */

            stuff *user_s = llist_find(llist_stuff,name,stuff_name_cmp);
            if(s == NULL)
            {
                stuff new_stuff ;
                memcpy(&new_stuff,s,sizeof(stuff));
                new_stuff.stuff_sum = num;
                
                //int flag1 = llist_insert(u -> llist_stuff_info,new_stuff_info);

                int flag2 = llist_insert(u->llist_stuff,&new_stuff);

                if(flag2 == -1)
                {
                    perror("insert");
                    sleep(3);
                    printf("\033c");
                }
                else if(flag2 == 0)
                {
                    printf("\033c");
                    printf("借出物资成功！\n");
                    s->stuff_num = s->stuff_sum - num;
                    sleep(1);
                    printf("\033c");
                    return 0;
                }

            }
            user_s->stuff_num += num;
            printf("借出物资成功！\n");
            s->stuff_num = s->stuff_num - num;
            sleep(1);
            printf("\033c");
        }    
        
        else
        {
            printf("此物资量不足 %d, 借出失败！\n", num);
        }  
        
    }
    return 0;
}


//普通用户 现借物资信息
void user_now_bor(user *u)
{
    struct llist_node *cur = (struct llist_node*)(u -> llist_stuff -> head.next);
    stuff *s;

    while (cur != &(u -> llist_stuff) -> head)
    {    
        s = (stuff *)cur -> data;
        printf("名称：%s  数量：%d  编号：%s\n",s->stuff_name,s->stuff_sum,s->stuff_id);
        cur = cur -> next;
    }
    printf("3s后自动退出...\n");
    sleep(3);
    printf("\033c");        
}


//初始化
void init(){
	printf("\033c");
    llist_user = llist_create(sizeof(user));
    llist_stuff = llist_create(sizeof(stuff));
}


//主菜单
void pri_menu(){
    static unsigned short m;
	while(1){
        printf("#####欢迎使用库存管理系统#####\n");
		printf("#####  1、管理员登陆     #####\n");
        printf("#####  2、普通用户登陆   #####\n");
        printf("#####  3、退出           #####\n");
        printf("##############################\n");
        printf("请输入数字：");
        scanf("%hd",&m);
        if(m==1){
            //管理员登陆
            printf("\033c"); //清空屏幕
            adm_logon();
        }
        else if(m==2){
            //普通用户登陆
            printf("\033c");
            user_logon();
        }
        else if(m==3){
            printf("\033c");
            printf("感谢使用本系统！\n");
            break;
        }
        else{
                printf("输入错误,请重新输入！\n\n");
        }
    }
}


//管理员菜单
void adm_menu()
{
    static unsigned short m;
	while(1)
    {
        printf("########管理员界面#########\n");	
		printf("####  1、修改用户名    ####\n");
        printf("####  2、修改密码      ####\n");
        printf("####  3、创建普通用户  ####\n"); 
        printf("####  4、删除普通用户  ####\n"); 
        printf("####  5、修改普通用户  ####\n"); 
        printf("####  6、查找普通用户  ####\n");
        printf("####  7、查看全部用户  ####\n");
        printf("####  8、添加物资信息  ####\n");
        printf("####  9、删除物资信息  ####\n");
        printf("####  10、查看所有物资 ####\n");
        printf("####  11、退出         ####\n");
        printf("###########################\n");
        printf("请输入数字：");
        scanf("%hd",&m);
        printf("\033c");
        if(m==1)
        {
            printf("\033c");
            printf("请输入新的用户名：");
            scanf("%s",adm_name);
            printf("更新成功！\n");
            sleep(2);
            printf("\033c");
        }
        else if(m==2)
        {
            printf("\033c");
            printf("请输入新的密码：");
            scanf("%s",adm_pass);
            printf("更新成功！\n");
            sleep(2);
            printf("\033c");
        }
        else if(m==3)
        {
            adm_add_user();//添加用户
        }
        else if(m==4)
        {
            adm_delete_user();//删除用户
        }
        else if(m==5)
        {
            adm_updata_user();//更新用户
        }
        else if(m==6)
        {
            find_user();//查找用户
        }
        else if(m==7)
        {
            find_all_user();//查看全部用户
        }
        else if(m==8)
        {
            add_stuff();//添加物资
        }
        else if(m==9)
        {
            delete_stuff();//删除物资
        }
        else if(m==10)
        {
            find_all_stuff();//查看所有物资
        }
        else if(m==11)
        {
            break;
        }
        else
        {
                printf("输入错误,请重新输入！\n\n");
        }
        printf("\033c");
    }
}


//用户菜单
void user_menu(user* u)
{
    static unsigned short m;
	while(1)
    {
        printf("#########用户菜单##########\n");	
		printf("####  1、修改个人信息 #####\n");
        printf("####  2、查询个人信息 #####\n");
        printf("####  3、借物资       #####\n"); 
        printf("####  4、还物资       #####\n");
        printf("####  5、现借物资记录 #####\n");    
        //printf("####  6、借还物资记录 #####\n");
        printf("####  6、退出         #####\n");
        printf("###########################\n");
        scanf("%hd",&m); 
        printf("\033c");
        if(m==1)
        {
           
            _updata_user_UI(u);
        }
        else if(m==2)
        {
            printf("工号：%d\n",u->job_num);
            printf("姓名：%s\n",u->u_name);
            printf("性别：%s\n",u->u_sex);
            printf("手机号：%s\n",u->u_mo_num);
            printf("身份证：%s\n",u->u_id);
            printf("密码：%s\n\n",u->u_passwd);

            printf("6s后自动退出界面....\n");
            
            sleep(6);

            printf("\033c");
        }
        else if(m==3)
        {
            user_bor(u);
        }
        else if(m==4)
        {
            
        }
        else if(m==5)
        {
            user_now_bor(u);
        }
        else if(m==6)
        {
            break;
        }
        /*else if(m==7)
        {
            
        }*/
        else
        {
            printf("输入错误,请重新输入！\n\n");
        }
    }
}


//管理员登陆
int adm_logon()
{
    char name[21],password[21];
    while(1)
    {   
        printf("#####管理员登陆界面#####\n");
        printf("请输入管理员用户名：");
        scanf("%s",name);
        printf("请输入管理员密码：");
        system("stty -echo");//禁止回显
        scanf("%s",password);
        system("stty echo");
        if(strcmp(name,adm_name) == 0 && strcmp(password,adm_pass) == 0)
        {
            printf("\033c");
            printf("登陆成功!\n");
            sleep(1);
            printf("\033c");
            adm_menu();
            return 0;
        }
    printf("\033c");
    printf("用户名或密码不存在,请重新登陆！\n");
    sleep(1);
    printf("\033c");
    }
    return 1;

}


//用户登陆
int user_logon()
{
    if(user_sum == 0)
    {
        printf("暂无用户，请添加后重试！\n");
        sleep(1);
        printf("\033c");
        return 1;
    }
    char name[21],password[21];
    struct llist_node *cur = llist_user -> head.next;

    while(cur != &llist_user -> head)
    {   
        printf("#####用户登陆界面#####\n");
        printf("请输入用户名：");
        scanf("%s",name);

        printf("请输入密码：");
        system("stty -echo");//禁止回显
        scanf("%s",password);
        system("stty echo");
        
        user *u;
        u = llist_find(llist_user,name,user_name_cmp);

        if(strcmp(password, u -> u_passwd) == 0)
        {
            printf("\033c");
            printf("登陆成功!\n");
            sleep(1);
            printf("\033c");
            user_menu(u);

            return 0;
        }
          
        printf("\033c");
        printf("用户名或密码不存在,请重新登陆！\n");
        sleep(1);
        printf("\033c");
    }
    return 1;
}


//读取数据
int data_read()
{
    
}


//管理员、物资数据保存
void stuff_save()
{
    
    FILE *pf_stuff=fopen("stuff.txt","w");
    fprintf(pf_stuff,"%s\n",adm_name);
    fprintf(pf_stuff,"%s\n",adm_pass);
    struct llist_node *cur = llist_stuff -> head.next;
    stuff s;
    while (cur != &llist_stuff -> head)
    {

        memcpy(&s,cur->data,sizeof(stuff));

        fprintf(pf_stuff,"%s\t%d\t%d\t%s\n",s.stuff_name,s.stuff_sum,s.stuff_num,s.stuff_id);
        cur = cur ->next;

    }

    fclose(pf_stuff);
}


//用户数据保存
void user_save()
{
    
    FILE *pf_user=fopen("user.txt","w");
    fprintf(pf_user,"%d\n",user_sum);

    struct llist_node *cur = llist_user -> head.next;
    user u;
    while (cur != &llist_user -> head)
    {

        memcpy(&u,cur->data,sizeof(user));

        fprintf(pf_user,"%d\t%s\t%s\t%s\t%s\t%s\n",u.job_num,u.u_name,u.u_sex,u.u_mo_num,u.u_id,u.u_passwd);
        cur = cur ->next;
        user_stuff_save(u.llist_stuff);
    }

    fclose(pf_user);
    
}

//物资数据保存
void user_stuff_save(LLIST *llist)
{
    FILE *pf_user=fopen("user.txt","a");
    struct llist_node *cur = llist -> head.next;
    stuff s;
    while (cur != &llist -> head)
    {

        memcpy(&s,cur->data,sizeof(stuff));

        fprintf(pf_user,"%s\t%d\t%d\t%s\n",s.stuff_name,s.stuff_sum,s.stuff_num,s.stuff_id);
        cur = cur ->next;
    }

}